/*
 *   AS IS License
 * ==Naive Coders==
 */
package org.naive.metawatch.converter.cap;

import java.text.ParseException;
import org.naive.metawatch.converter.parser.cap.CapParseContext;
import org.naive.metawatch.converter.parser.cap.CapParseable;

/**
 *
 * @author gbu.000
 */
public class CapMethodHeaderInfo extends CapParseable {

    public byte bitfield;
    public byte bitfield_orMax_stack;
    public byte nargs;
    public byte max_locals;

    public boolean isExtended() {
        return ((bitfield >> 4 & 0xf) & 0x8) != 0;
    }

    public boolean isAbstract() {
        return ((bitfield >> 4 & 0xf) & 0x4) != 0;
    }

    public byte getMaxStack() {
        if (isExtended()) {
            return bitfield_orMax_stack;
        } else {
            return (byte) (bitfield & 0xf);
        }
    }

    public byte getNargs() {
        if (isExtended()) {
            return nargs;
        } else {
            return (byte) (bitfield_orMax_stack >> 4 & 0xf);
        }
    }

    public byte getMaxLocals() {
        if (isExtended()) {
            return max_locals;
        } else {
            return (byte) (bitfield_orMax_stack & 0xf);
        }
    }

    @Override
    public void parse(CapParseContext px) throws ParseException {
        bitfield = px.readByte();
        bitfield_orMax_stack = px.readByte();
        if (isExtended()) {
            nargs = px.readByte();
            max_locals = px.readByte();
        }
    }
}
